package com.nidu.demo.company.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.PageQuery;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nidu.demo.company.dto.CompanyListQry;
import com.nidu.demo.company.dto.CompanyPageQry;
import com.nidu.demo.company.convertor.CompanyConvertor;
import com.nidu.demo.company.gateway.CompanyGateway;
import com.nidu.demo.company.mapper.CompanyMapper;
import com.nidu.demo.company.mapper.dataobject.CompanyDO;
import com.nidu.demo.company.model.Company;
import com.nidu.demo.company.model.CompanyCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Set;
import java.util.List;

@Component
@RequiredArgsConstructor
public class CompanyGatewayImpl implements CompanyGateway {

    private final CompanyMapper mapper;
    private final CompanyConvertor convertor;

    @Override
    public Company create(Company company) {
        CompanyDO companyDO = convertor.toDataObject(company);
        mapper.insert(companyDO);
        return convertor.toDomainObject(companyDO);
    }

    @Override
    public Company update(Company company) {
        CompanyDO companyDO = convertor.toDataObject(company);
        mapper.updateById(companyDO);
        return convertor.toDomainObject(companyDO);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Set<Long> ids) {
        mapper.deleteByIds(ids);
    }

    @Override
    public Company getByName(String name) {
        CompanyDO companyDO = mapper.selectByName(name);
        if(companyDO == null){
            return null;
        }
        return convertor.toDomainObject(companyDO);
    }

    @Override
    public Company getById(Long id) {
        CompanyDO companyDO = mapper.selectById(id);
        if (companyDO == null) {
            return null;
        }
        return convertor.toDomainObject(companyDO);
    }

    @Override
    public List<Company> getByIds(Set<Long> ids) {
        LambdaQueryWrapper<CompanyDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CompanyDO::getId, ids);
        List<CompanyDO> companyDOS = mapper.selectList(wrapper);
        if(CollUtil.isEmpty(companyDOS)){
            return null;
        }
        return convertor.toDomainObjects(companyDOS);
    }

    @Override
    public List<Company> listByCondition(CompanyCriteria criteria) {
        LambdaQueryWrapper<CompanyDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(criteria.getStatus() != null, CompanyDO::getStatus, criteria.getStatus());
        wrapper.in(!CollUtil.isEmpty(criteria.getCompanyIds()), CompanyDO::getId, criteria.getCompanyIds());

        wrapper.like(!StrUtil.isEmpty(criteria.getCompanyName()), CompanyDO::getName, criteria.getCompanyName());
        // 可以根据需要添加更多查询条件
        List<CompanyDO> companyDOS = mapper.selectList(wrapper);
        return CollUtil.isEmpty(companyDOS) ? new ArrayList<>() : convertor.toDomainObjects(companyDOS);
    }

    @Override
    public PageResponse<Company> pageByCondition(CompanyCriteria criteria) {
        // 创建查询条件
        LambdaQueryWrapper<CompanyDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(criteria.getStatus() != null, CompanyDO::getStatus, criteria.getStatus());

        LocalDateTime[] createTime = criteria.getCreateTime();
        if (createTime != null) {
            if (createTime.length == 1 && createTime[0] != null) {
                wrapper.ge(CompanyDO::getCreateTime, createTime[0]);
            } else if (createTime.length > 1 && createTime[0] != null && createTime[1] != null) {
                wrapper.between(CompanyDO::getCreateTime, createTime[0], createTime[1]);
            }
        }
        wrapper.like(!StrUtil.isEmpty(criteria.getCompanyName()), CompanyDO::getName, criteria.getCompanyName());

        // 创建分页对象，确保使用 CompanyDO
        Page<CompanyDO> page = new Page<>(criteria.getPageIndex(), criteria.getPageSize());

        // 排序字段
        if (!StrUtil.isEmpty(criteria.getOrderBy())) {
            if (PageQuery.DESC.equals(criteria.getOrderDirection())) {
                page.addOrder(OrderItem.desc(criteria.getOrderBy()));
            } else {
                page.addOrder(OrderItem.asc(criteria.getOrderBy()));
            }
        }
        mapper.selectPage(page, wrapper);
        List<Company> companies = convertor.toDomainObjects(page.getRecords());
        return PageResponse.of(companies, (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        CompanyDO companyDO = new CompanyDO();
        companyDO.setId(id);
        companyDO.setStatus(status);
        mapper.updateById(companyDO);
    }
}